package com.coders.android.hibernate.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.coders.android.hibernate.dao.PeticionDAO;
import com.coders.android.hibernate.dto.Peticion;

public class PeticionDAOHibernate extends HibernateDaoSupport implements PeticionDAO {

	@Override
	public void insertar(Peticion peticion) {

		Transaction transaction = null;
		try{
			Session sesion = getSession();
			
			transaction = sesion.beginTransaction();
			sesion.save( peticion );
			transaction.commit();
			
		}catch (HibernateException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void eliminar(String imeiUsuario, String imeiTaxi) {

		Transaction transaction = null;
		try{
			Session sesion = getSession();
			
			transaction = sesion.beginTransaction();

			String hqlQuery = ""
					+ "DELETE FROM Peticion "
					+ "WHERE imeiUsuario = :imeiUsuario AND imeiTaxi = :imeiTaxi";
			
			sesion.createQuery( hqlQuery )
					.setString( "imeiUsuario", imeiUsuario )
					.setString( "imeiTaxi", imeiTaxi ).executeUpdate();		
			
			transaction.commit();
			
		}catch (HibernateException e) {
			e.printStackTrace();
		}
	}

}
